package config

import (
	"os"

	"gopkg.in/yaml.v3"
)

type Config struct {
	Server      ServerConfig `yaml:"server"`
	DB          DBConfig     `yaml:"db"`
	Redis       RedisConfig  `yaml:"redis"`
	JWT         JWTConfig    `yaml:"jwt"`
	Email       EmailConfig  `yaml:"email"`
	OtherConfig OtherConfig  `yaml:"otherconfig"`
}

type ServerConfig struct {
	Port string `yaml:"port"`
}
type DBConfig struct {
	DB       string `yaml:"db"`
	Host     string `yaml:"host"`
	Port     string `yaml:"port"`
	User     string `yaml:"user"`
	Password string `yaml:"password"`
	DBName   string `yaml:"dbname"`
}
type RedisConfig struct {
	Host     string `yaml:"host"`
	Port     string `yaml:"port"`
	Password string
}
type JWTConfig struct {
	Secret string `yaml:"secret"`
}
type EmailConfig struct {
	Host     string `yaml:"host"`
	Port     string `yaml:"port"`
	User     string `yaml:"user"`
	Password string `yaml:"password"`
}
type OtherConfig struct {
}

func NewConfig() Config {
	file, err := os.Open("./app.yml")
	if err != nil {
		panic(err)
	}
	defer func(file *os.File) {
		err := file.Close()
		if err != nil {

		}
	}(file)
	decoder := yaml.NewDecoder(file)
	var cfg Config
	err = decoder.Decode(&cfg)
	if err != nil {
		panic(err)
	}
	return cfg
}
